﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Xml.Linq;
using System.Text.RegularExpressions;


namespace HtmlExtractor.Config
{
   
    public abstract class ReplaceExpression
    {
        public RegexConfig FindRegex;
        internal abstract string GetReplacementText(string inputText, ParserContext context);
        public static ReplaceExpression Create(XElement replaceNode)
        {
            if (replaceNode.Element("ProcessedReplace") != null)
            {
                return new ProcessedReplace(replaceNode);
            }
            else
                return new DirectReplace(replaceNode);
        }
      /// <summary>
      /// 
      /// </summary>
      /// <param name="input"></param>
      /// <param name="createReplaceInputText"></param>
      /// <param name="context"></param>
      /// <returns></returns>
        public abstract string GetResult(string input, ParserContext context);
    }
    public class DirectReplace:ReplaceExpression
    {
        public string ReplacementText="";
        public DirectReplace()
        { }
        public DirectReplace(XElement replaceNode)
        {
            FindRegex =new RegexConfig(replaceNode.Element("FindRegex"));
            ReplacementText = replaceNode.Element("DirectReplace")==null?"":(string)replaceNode.Element("DirectReplace");
        }
        internal override string GetReplacementText(string inputText, ParserContext context)
        {
            return ReplacementText;
        }
        public override string GetResult(string input, ParserContext context)
        {
            return SegmentRegex.Replace(input, FindRegex.Expression,ReplacementText,FindRegex.Options);
            
        }
    }
    /// <summary>
    ///  只支持单个结果
    /// </summary>
    public class ProcessedReplace : ReplaceExpression
    {
       //private readonly ILog log = LogManager.GetLogger(typeof(ProcessedReplace));
        /// <summary>
        /// Context中的路径
        /// </summary>
        public string InputPath;
        /// <summary>
        /// 包含需要替换{0}的字符
        /// </summary>
        public string FormateReplacementText;
        public ProcessedReplace()
        { }
        public ProcessedReplace(XElement replaceNode)
        {
            FindRegex = new RegexConfig(replaceNode.Element("FindRegex"));
            XElement processedNode = replaceNode.Element("ProcessedReplace");
            InputPath = (string)processedNode.Attribute("inputPath");
            FormateReplacementText = (string)processedNode.Element("ReplacementText");
            SingleResultMatch= RuleBuildFactory.Create((XElement)processedNode.LastNode);
        }
        /// <summary>
        /// 只取单个结果
        /// </summary>
        public IPageItemConfig SingleResultMatch;

        internal override string GetReplacementText(string createReplaceInputText, ParserContext context)
        {
            string input = "";
            if (string.IsNullOrEmpty(InputPath))
            {
                input = createReplaceInputText;
            }
            else if (InputPath == "PageUri")
                input = context.ParseUri;
            else
                throw new NotSupportedException();
            PageResult result = SingleResultMatch.GetParseResult(input, context);
            if (result.Count != 0)
            {
                IPageItem pi = result[0];
                List<string> o = new List<string>();
                if (pi is SimpleItem)
                {
                    SimpleItem s = pi as SimpleItem;
                    o.Add(s.Value);
                }
                else
                {
                    CompositeItem c = pi as CompositeItem;
                    o = c.GetValue();
                }
                string rep = string.Format(FormateReplacementText, o.ToArray());
                //log.Info("replacetext:" + rep);
                return rep;
            }
            else
            {
                //log.Info("没有替换表达式"+input);
                return string.Format(FormateReplacementText,""); 
            }
           
        }
        public override string GetResult(string input, ParserContext context)
        {
            //return SegmentRegex.Replace(input, FindRegex.Expression, ReplacementText, FindRegex.Options);
            string replaceText=GetReplacementText(input, context);
            return SegmentRegex.Replace(input, FindRegex.Expression, replaceText, FindRegex.Options);
        }
    }
  
}
